package com.tianci.appstore;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.skyworth.framework.skysdk.ipc.SkyContext;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.controller.AppStoreSearchController;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.search.SearchListView;
import com.tianci.appstore.ui.search.SearchMainLayout;

import java.util.HashMap;
import java.util.Map;

public class SearchActivity extends SkyBaseActivity {
    private LinearLayout main;
    private SearchListView mSearchListView = null;
    private SearchMainLayout mInputLayout = null;
    public static final String SEARCH_KEY = "search_key";
    private static String mKey = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SkyContext.context = getApplicationContext();
        main = new LinearLayout(this);
        main.setOrientation(LinearLayout.HORIZONTAL);
        SkinManager.setBackground(main, R.drawable.ui_sdk_app_bg);
        setContentView(main);
        AppStoreSearchController.getAppStoreSearchController().init();
        mKey = getIntent().getStringExtra(SEARCH_KEY);
        if (mKey == null) {
            mKey = "";
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mKey = intent.getStringExtra(SEARCH_KEY);
        if (mInputLayout != null) {
            mInputLayout.clearValue();
            if (mKey == null) {
                mKey = "";
            }
            mInputLayout.addValue(mKey);
        }
    }

    @Override
    protected Map<String, String> getPageStartParams() {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("search_key", mKey);
        return map;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            String key = "";
            switch (keyCode) {
                case KeyEvent.KEYCODE_0:
                    key = "0";
                    break;
                case KeyEvent.KEYCODE_1:
                    key = "1";
                    break;
                case KeyEvent.KEYCODE_2:
                    key = "2";
                    break;
                case KeyEvent.KEYCODE_3:
                    key = "3";
                    break;
                case KeyEvent.KEYCODE_4:
                    key = "4";
                    break;
                case KeyEvent.KEYCODE_5:
                    key = "5";
                    break;
                case KeyEvent.KEYCODE_6:
                    key = "6";
                    break;
                case KeyEvent.KEYCODE_7:
                    key = "7";
                    break;
                case KeyEvent.KEYCODE_8:
                    key = "8";
                    break;
                case KeyEvent.KEYCODE_9:
                    key = "9";
                    break;
                case 0:
                case KeyEvent.KEYCODE_DEL:
                    if (mInputLayout != null) {
                        mInputLayout.deleteValue();
                    }
                    return true;
                default:
                    break;
            }
            if (!key.equals("") && mInputLayout != null) {
                mInputLayout.addValue(key);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void addContentLayout() {
        mInputLayout = new SearchMainLayout(this, 1);
        mInputLayout.initUI();
        main.addView(mInputLayout);
        mSearchListView = new SearchListView(this, ListType.SEARCH_LIST);
        mSearchListView.createView();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        main.addView(mSearchListView, params);
        mInputLayout.setSearchKeyChangeListener(mSearchListView.getController());
        mInputLayout.setClearFocusListener(mSearchListView.getController());
        mSearchListView.getController().setSearchResultListener(mInputLayout);
        mInputLayout.addValue(mKey);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSearchListView != null)
            mSearchListView.destroyController();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus && mSearchListView == null) {
            addContentLayout();
            initAudio();
        }
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public IAudio getAudioInterface() {
        return mSearchListView.getAudioInterface();
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "应用圈搜索页面";
    }
}
